package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录业务：userLoginParam={}",userLoginParam);
        UserVO userVO= userMapper.selectUserByUname(userLoginParam.getUsername());
        if (userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam){
        log.debug("保存用户业务：userSaveParam={}",userSaveParam);
        User user=new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if (user.getId()==null){
            user.setPassword("123456");
            user.setCreateTime(new Date());
            int i = userMapper.insertUser(user);
            if (i!=1){
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
        }else if (user.getUsername()==null){
            int i = userMapper.updatePwd();
            if (i!=1)
                throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else {
            user.setUpdateTime(new Date());
            int i = userMapper.updateUser(user);
            if (i != 1) {
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
        }
    }

    @Override
    public List<User> getAllUser() {
        List<User> allUser = userMapper.getAllUser();
        return allUser;
    }



    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("查询用户业务:userQuery={}",userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        return list;
    }


    @Override
    public void resetPwd(Long id) {
        log.debug("重置密码业务： id={}",id);
        User user=new User();
        user.setId(id);
        user.setUpdateTime(new Date());
        user.setPassword("123456");
        int i = userMapper.update(user);
        if (i!=1){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
    }

    @Override
    public void updateStatus(Long userId, String status) {
        User user=new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        int i = userMapper.update(user);
        if (i!=1)
            throw new ServiceException(StatusCode.OPERATION_FAILED);
    }

    @Override
    public void deleteUser(Long id) {
        int i = userMapper.deleteUser(id);
        if (i!=1)
            throw new ServiceException(StatusCode.OPERATION_FAILED);
    }
}
